package com.wzt.consumer.controller;

import com.wzt.base.util.AjaxResponse;
import com.wzt.consumer.dto.SmsCodeRegisterDto;
import com.wzt.consumer.service.IVerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author Zenith
 */
@RestController
@RequestMapping("/validate")
public class ValidateController {
    @Autowired
    private IVerificationCodeService verificationCodeService;

    /**
     * 图形验证码
     *
     * @param key
     * @return
     */
    @GetMapping("/img/{key}")
    public AjaxResponse imageCode(@PathVariable String key) {
        try {
            String imageCode = verificationCodeService.getImageCode(key);
            return AjaxResponse.oneResponse().setData(imageCode);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage(e.toString());
        }
    }

    /**
     * 短信验证码
     *
     * @return
     */
    @PostMapping("/sms/register")
    public AjaxResponse smsRegisterCode(@RequestBody SmsCodeRegisterDto smsCodeDto) {
        try {
            return verificationCodeService.sendRegisterSmsCode(smsCodeDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("发送失败！" + e.toString());
        }
    }
}
